// pages/login/forgetpswd/forgetpswd.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 输入的手机号
    tele: '',
    formError: false,
    errorMessage: '',
    changetime: '发送验证码',
    // 输入的验证码
    inputcheckpswd: '',
    //输入的新密码
    pswdInput: '',
    //输入的确认新密码
    pswdagainInput: '',
    //获取到的验证码
    getcheckpswd: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  // 获取验证码
  sendcheckword: function (e) {
    var that = this
    console.log(this.data.tele)
    var pattern = /0?(13|14|15|18|17|19)[0-9]{9}/;

    var tele = this.data.tele;

    if (this.data.changetime != '发送验证码') {
      this.showRegError('请' + that.data.changetime + '秒后再试');
      return
    }

    if (tele) {
      if (!pattern.test(tele)) {
        this.showRegError('请输入正确的手机号');
        return;
      } else {
        wx.request({
          url: 'https://www.lanshuicar.com/Server/FindPassword.ashx',
          data: {
            Meth: 0,
            Tel: tele
          },
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          // header: {}, // 设置请求的 header
          success: function (res) {
            // success
            console.log(res);
            var code = res.data.code;
            if (code == 0) {
              wx.showToast({
                title: '发送成功',
                icon: 'success',
                duration: 2000
              })
              that.changetime('121');
              that.setData({
                getcheckpswd: res.data.data[0].CodeNum
              });
              console.log(that.data.getcheckpswd)
            } else {
              that.showRegError(res.data.msg);
            }
          },
          fail: function () {
            // fail
          },
          complete: function () {
            // complete
          }
        })
      }
    } else {
      that.showRegError('请输入正确的手机号');
    }
  },

  showRegError: function (msg) {
    var that = this;
    that.setData({
      formError: true,
      errorMessage: msg
    });

    setTimeout(function () {
      that.setData({
        formError: false,
        errorMessage: '未知错误'
      });
    }, 2000);
  },

  teleInput: function (e) {
    console.log(e)
    var tele = e.detail.value;
    if (tele != '') {
      this.setData({
        tele: tele
      });
    }
  },

  checkpswdInput: function (e) {
    console.log(e)
    var inputcheckpswd = e.detail.value;
    if (inputcheckpswd != '') {
      this.setData({
        inputcheckpswd: inputcheckpswd
      });
    }
  },

  pswdInput: function (e) {
    console.log(e)
    var pswdInput = e.detail.value;
    if (pswdInput != '') {
      this.setData({
        pswdInput: pswdInput
      });
    }
  },

  pswdagainInput: function (e) {
    console.log(e)
    var pswdagainInput = e.detail.value;
    if (pswdagainInput != '') {
      this.setData({
        pswdagainInput: pswdagainInput
      });
    }
  },

  changetime: function (startnum) {
    var that = this
    var second = startnum
    if (second != 1) {
      second--
      this.setData({
        changetime: second
      });
    } else {
      this.setData({
        changetime: '发送验证码'
      });
      return
    }

    setTimeout(function () {
      that.changetime(second)
    }, 1000);
  },

  sendnewpswd: function () {
    var that = this
    console.log(1111111)
    // 第一步验证是否都输入了
    if (that.data.tele == '') {
      that.showRegError('请输入正确的手机号');
      return
    }
    if (that.data.inputcheckpswd == '') {
      that.showRegError('请输入验证码');
      return
    }
    if (that.data.pswdInput == '') {
      that.showRegError('请输入新密码');
      return
    }
    if (that.data.pswdInput.length < 6) {
      that.showRegError('新密码至少6位');
      return
    }
    if (that.data.pswdagainInput == '') {
      that.showRegError('请确认密码');
      return
    }
    if (that.data.pswdInput != that.data.pswdagainInput) {
      that.showRegError('两次密码不一致');
      return
    }
    if (that.data.getcheckpswd == '') {
      that.showRegError('请获取验证码');
      return
    }

    wx.request({
      url: 'https://www.lanshuicar.com/Server/FindPassword.ashx',
      data: {
        Meth: 2,
        Tel: that.data.tele,
        NewPwd: that.data.pswdInput,
        Code: that.data.getcheckpswd
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        console.log(res);
        var code = res.data.code;
        if (code == 0) {
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 2000
          })
          var pages = getCurrentPages();
          var currPage = pages[pages.length - 1];  //当前页面
          var prevPage = pages[pages.length - 2]; //上一个页面

          //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
          prevPage.setData({
            forgetpswd: '1'
          })

          wx.navigateBack();

        } else {
          that.showRegError(res.data.msg);
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  }
})